package com.example.lin.start;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @className: ExcelZipDownload
 * @description:
 * @author: Lin.Gu
 * @date: 2024/6/14
 */
public class ExcelZipDownload {

    public static void main(String[] args) {
        try {
            // 创建第一个Excel文件
            Workbook workbook1 = new XSSFWorkbook();
            Sheet sheet1 = workbook1.createSheet("Sheet1");
            Row row1 = sheet1.createRow(0);
            Cell cell1 = row1.createCell(0);
            cell1.setCellValue("Hello from Excel 1");

            // 保存第一个Excel文件到临时位置
            String tempFile1 = "D:\\test\\temp_excel1.xlsx";
            FileOutputStream outputStream1 = new FileOutputStream(tempFile1);
            workbook1.write(outputStream1);
            workbook1.close();
            outputStream1.close();

            // 创建第二个Excel文件
            Workbook workbook2 = new XSSFWorkbook();
            Sheet sheet2 = workbook2.createSheet("Sheet1");
            Row row2 = sheet2.createRow(0);
            Cell cell2 = row2.createCell(0);
            cell2.setCellValue("Hello from Excel 2");

            // 保存第二个Excel文件到临时位置
            String tempFile2 = "D:\\test\\temp_excel2.xlsx";
            FileOutputStream outputStream2 = new FileOutputStream(tempFile2);
            workbook2.write(outputStream2);
            workbook2.close();
            outputStream2.close();

            // 将两个Excel文件打包成ZIP
            String zipName = "D:\\test\\excels.zip";
            FileOutputStream fos = new FileOutputStream(zipName);
            ZipOutputStream zos = new ZipOutputStream(fos);

            addToZipFile(tempFile1, zos);
            addToZipFile(tempFile2, zos);

            zos.close();
            fos.close();

            System.out.println("ZIP file created successfully.");

//            File file1 = new File(tempFile1);
//            File file2 = new File(tempFile2);
//            if (file1.exists()) {
//                file1.delete();
//            }
//            if (file2.exists()) {
//                file2.delete();
//            }
//            System.out.println("Excel files deleted successfully.");
            // 注意：在实际应用中，您可能需要考虑删除临时的Excel文件

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void addToZipFile(String fileName, ZipOutputStream zos) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.getName());
        ZipEntry zipEntry = new ZipEntry("test001"+System.currentTimeMillis()+".xlsx");
        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }
}
